package fun.shuofeng.juc.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 朔风
 * @date 2024-01-28 16:33
 */
public class ThreadDemo3 {
    public static void main(String[] args) {
        ShareResource shareResource = new ShareResource();

        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    shareResource.print5(i);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "AA").start();
        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    shareResource.print10(i);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "BB").start();
        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                try {
                    shareResource.print15(i);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "CC").start();

    }
}

class ShareResource {
    // 定义标志位
    private int flag = 1;
    // 创建锁
    private Lock lock = new ReentrantLock();

    // 创建condition
    private Condition c1 = lock.newCondition();
    private Condition c2 = lock.newCondition();
    private Condition c3 = lock.newCondition();

    /**
     * 打印5次
     *
     * @param loop 轮次
     * @throws InterruptedException
     */
    public void print5(int loop) throws InterruptedException {
        // 上锁
        lock.lock();
        try {
            // 判断
            while (flag != 1) {
                c1.await();
            }
            // 干活
            for (int i = 1; i <= 5; i++) {
                System.out.println(Thread.currentThread().getName() + " :: " + i + " 轮次: " + loop);
            }
            flag = 2;
            // 通知BB线程
            c2.signal();
        } finally {
            // 释放锁
            lock.unlock();
        }
    }

    /**
     * 打印10次
     *
     * @param loop 轮次
     * @throws InterruptedException
     */
    public void print10(int loop) throws InterruptedException {
        // 上锁
        lock.lock();
        try {
            // 判断
            while (flag != 2) {
                c2.await();
            }
            // 干活
            for (int i = 1; i <= 10; i++) {
                System.out.println(Thread.currentThread().getName() + " :: " + i + " 轮次: " + loop);
            }
            flag = 3;
            // 通知CC线程
            c3.signal();
        } finally {
            // 释放锁
            lock.unlock();
        }
    }

    /**
     * 打印15次
     *
     * @param loop 轮次
     * @throws InterruptedException
     */
    public void print15(int loop) throws InterruptedException {
        // 上锁
        lock.lock();
        try {
            // 判断
            while (flag != 3) {
                c3.await();
            }
            // 干活
            for (int i = 1; i <= 15; i++) {
                System.out.println(Thread.currentThread().getName() + " :: " + i + " 轮次: " + loop);
            }
            flag = 1;
            // 通知AA线程
            c1.signal();
        } finally {
            // 释放锁
            lock.unlock();
        }
    }


}
